capture log close

// program: 
// task: 	
// project:	LAPS Replication
// author: 	Tom Mustillo, 2018-01-27
// note: 	

version 13
clear all
set linesize 80
macro drop _all

// DATA MANAGEMENT	 
	use Mustillo.LAPS.2018.dta
	sort Country Party District Election
	
// FINAL MODELS
// Reported in a table of results in the suppplementary appendix
// Predictions of random effects and residuals are used to make all Figures.
// Model fitting and comparison tests (based upon BIC and AIC) not reported. 

//Bolivia(cid 22): 
	*best fitting model:
	xtmixed vp y y2 y3||pid: y y2 y3, cov(unstr) ||did:y if cid==22 , ///
	var mle cov(unstr)
	estat ic
	*save predicted values
	capture drop pvp resid pvpP
	predict pvp, fit
	predict resid, res
	predict pvpP, fit level(pid)
	set scheme s2manual
	*Plot party level results: Figure 1
	sort cid pid did Election
	twoway ///
		(line pvpP Election if cid==22 & Party=="ADN", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==22 & Party=="AS", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==22 & Party=="CONDEPA", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="IU", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="MAS", lpattern(solid) lwidth(thick) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="MBL", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="MIP", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="MIR", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="MNR", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==22 & Party=="MNRI", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==22 & Party=="MRTKL", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==22 & Party=="MSM", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==22 & Party=="NFR", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==22 & Party=="PDC(2014)", msymbol(O) mc(gs0)) ///
		(sc pvpP Election if cid==22 & Party=="PLJ", msymbol(O) mc(gs0)) ///
		(sc pvpP Election if cid==22 & Party=="PODEMOS", msymbol(O) mc(gs0)) ///
		(sc pvpP Election if cid==22 & Party=="PPB-CN", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==22 & Party=="PS-1", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==22 & Party=="PVB", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==22 & Party=="UCS", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="UD", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="VR-9", lpattern(solid) connect(ascending)), ///
		xtitle("Election") ytitle("% Vote") legend(off) ///
		xlabel(1985(5)2015) ylabel(0(10)60) legend(off) xline(2002) ysize(3) xsize(6.5)
	*Plot district level results: Figure 2
	sort cid pid did Election
	twoway   ///
		(line pvpP Election if cid==22 & (Party=="MAS" | Party=="MNR"), connect(ascending) lwidth(thick)) ///
		(line pvp Election if cid==22 & Party=="MAS", connect(ascending)) ///
		(line pvp Election if cid==22 & Party=="MNR", connect(ascending)),  ///
		xlabel(1985(5)2015) ylabel(0(10)80) ysize(2.5) xsize(6.5) ///
		xtitle("Election") ytitle("% Vote") legend(pos(7) row(1) order(1 "Party Means" 2 "MAS" 3 "MNR")) xline(2002)

// Ecuador (cid 25)
	*best fitting model
	xtmixed vp y y2 y3 ||pid: y y2 y3, cov(unstr) ||did:y y2 if cid==25 , ///
	var mle cov(unstr)
	estat ic
	*save predicted values
	capture drop pvp resid pvpP
	predict pvp, fit
	predict resid, res
	predict pvpP, fit level(pid)
	set scheme s2manual
	*Plot party level results: Figure 1	
	sort cid pid did Election
	twoway ///
		(line pvpP Election if cid==25 & Party=="AN", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="APRE", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="Avanza", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="CFP", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="CREO", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="FADI", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="FNV", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="ID", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="LP", msymbol(O) mc(gs0)) ///
		(sc pvpP Election if cid==25 & Party=="MCMG", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="MCNP", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="MG", msymbol(O) mc(gs0)) ///
		(sc pvpP Election if cid==25 & Party=="MMIN", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="MPD", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="MPS", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="MUPP-NP", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PAIS", lpattern(solid) lwidth(thick) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PCD", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PD", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="PL", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="PLRE", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PNR", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PR", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PRE", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PRIAN", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PS-FA", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PSC", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PSP", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PUR", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="Primero", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="RED", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="Ruptura", msymbol(O) mc(gs0)) ///
		(sc pvpP Election if cid==25 & Party=="SUMA", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="UDC", lpattern(solid) connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="UL", msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="UN-UNO", lpattern(solid) connect(ascending)), ///
		xtitle("Election") ytitle("% Vote") legend(off) ///
		xlabel(1978(4)2014) ylabel(0(10)40) xline(2006) ysize(3) xsize(6.5)
	*Plot district level results: Figure 2
	sort cid pid did Election
		twoway   ///
		(line pvpP Election if cid==25 & Party=="ID", connect(ascending) lwidth(thick) lpattern(solid)) ///
		(line pvp Election if cid==25 & Party=="PAIS", connect(ascending)) ///
		(line pvp Election if cid==25 & Party=="ID", connect(ascending))  ///
		(line pvpP Election if cid==25 & Party=="PAIS", connect (ascending) lwidth(thick) lpattern(solid)), ///
		xlabel(1978(4)2014) ylabel(0(10)50) ysize(2.5) xsize(6.5) ///
		xtitle("Election") ytitle("% Vote") legend(pos(7) row(1)order(1 "Party Means" 2 "PAIS" 3 "ID")) xline(2006)

// Venezuela (cid 21) 
	*best fitting model
	xtmixed vp y y2 y3||pid: y y2 y3, cov(unstr) ||did:y  if cid==21 , cov(unstr) var mle
	estat ic
	*save predicted values
	capture drop pvp resid pvpP
	predict pvp, fit
	predict resid, res
	predict pvpP, fit level(pid)
	set scheme s2manual
	*Plot party level results: Figure 1
	sort cid pid did Election
	twoway ///
		(line pvpP Election if cid==21 & Party=="AD", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="CCN(1)", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="CONVERGENCIA", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="COPEI", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="FDP(1)", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="FND(1)", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="LCR", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="MAS", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="MEP", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="MIR", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="MPJ", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="NGD", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="PCV", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="PODEMOS", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="PPT", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="PRIN", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="PRVZL", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="PSUV", lpattern(solid) lwidth(thick) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="UNT", lpattern(solid) connect(ascending)) ///
		(line pvpP Election if cid==21 & Party=="URD", lpattern(solid) connect(ascending)), ///
		xtitle("Election") ytitle("% Vote") xlabel(1958(5)2013) ylabel(0(10)60) ///
		legend(off) xline(1998) ysize(2.5) xsize(6.5)
	*Plot district level results: Figure 2
	sort cid pid did Election
		twoway   ///
		(line pvpP Election if cid==21 & (Party=="PSUV" | Party=="AD"), connect(ascending) lwidth(thick)) ///
		(line pvp Election if cid==21 & Party=="PSUV", connect(ascending)) ///
		(line pvp Election if cid==21 & Party=="AD", connect(ascending)),  ///
		xlabel(1958(5)2013) ylabel(0(10)70) ysize(2.5) xsize(6.5) ///
		xtitle("Election") ytitle("% Vote")  legend(pos(7) row(1) order(1 "Party Means" 2 "PSUV" 3 "AD")) xline(1998)

*Party Level Trajectories, For Supplementary Info with Legend.
	twoway ///
		(line pvpP Election if cid==22 & Party=="MAS", connect(ascending)) ///
		(line pvpP Election if cid==22 & Party=="ADN", connect(ascending)) ///
		(connected pvpP Election if cid==22 & Party=="MIR", connect(ascending) msymbol(th) mc(gs0) lpattern(solid)) ///
		(line pvpP Election if cid==22 & Party=="MNR", connect(ascending)) ///
		(connected pvpP Election if cid==22 & Party=="NFR", connect(ascending) msymbol(oh)) ///
		(line pvpP Election if cid==22 & Party=="UCS", connect(ascending)) ///
		(connected pvpP Election if cid==22 & Party=="UD",  connect(ascending) msymbol(dh) mc(gs0) lpattern(solid)) ///
		(sc pvpP Election if cid==22 & Party=="PPB-CN",  msymbol(O) mc(gs0)) ///
		(sc pvpP Election if cid==22 & Party=="PODEMOS",  msymbol(T) mc(gs0)), ///
		xtitle("Election") ytitle("% Vote") ///
		xlabel(1985(5)2015) ylabel(0(10)60) xline(2002) ysize(3) xsize(6.5) ///
		leg(order(1 "MAS" 2 "ADN" 3 "MIR" 4 "MNR" 5 "NFR" 6 "UCS" 7 "UD" 8 "PPB-CN" 9 "PODEMOS") pos(6) c(5)) 
	twoway ///
		(line pvpP Election if cid==25 & Party=="PAIS", connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="CFP", connect(ascending)) ///
		(sc pvpP Election if cid==25 & Party=="CREO",  msymbol(O) mc(gs0)) ///
		(line pvpP Election if cid==25 & Party=="ID", connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PRE", connect(ascending)) ///
		(line pvpP Election if cid==25 & Party=="PRIAN", connect(ascending)) ///
		(connected pvpP Election if cid==25 & Party=="PSC", connect(ascending) lpattern(solid) msymbol(t) mc(gs0)) ///
		(connected pvpP Election if cid==25 & Party=="PSP", connect(ascending) lpattern(solid) msymbol(Th) mc(gs0)) ///
		(connected pvpP Election if cid==25 & Party=="UDC", connect(ascending)lpattern(solid) msymbol(d) mc(gs0)) ///
		(connected pvpP Election if cid==25 & Party=="UN-UNO", connect(ascending) lpattern(solid) msymbol(Oh) mc(gs0)), ///
		xtitle("Election") ytitle("% Vote") ///
		xlabel(1978(4)2014) ylabel(0(10)40) xline(2006) ysize(3) xsize(6.5) ///
		leg(order(1 "PAIS" 2 "CFP" 3 "CREO" 4 "ID" 5 "PRE" 6 "PRIAN" 7 "PSC" 8 "PSP" 9 "UDC" 10 "UN-UNO") pos(6) c(5)) 
	xtline pvpP if cid==21 & (Party=="AD" | Party=="COPEI" | Party=="URD" | Party=="PSUV" | Party=="CONVERGENCIA" | Party=="MEP"), t(Election) i(Party) overlay xtitle("Election") ytitle("% Vote") xlabel(1958(5)2013) ylabel(0(10)60) xline(1998) leg(order(5 "PSUV" 1 "AD" 2 "CONV" 3 "COPEI" 4 "MEP" 6 "URD") pos(6) c(6)) ysize(3) xsize(6.5)

log close
exit
